﻿using System;
using System.Data;
using System.Data.SqlClient;
using DataAccess;

namespace Bussiness
{
    public class Account
    {
        private SqlConnection conn = null;

        public int NewAccountAdmin(EntityAccountAdmin account)
        {
            try
            {
                account = new EntityAccountAdmin();
                conn = AppConfig.GetConnection();
                SqlCommand cmd = new SqlCommand("proc_name", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@FullName", account.FullName);
                cmd.Parameters.Add("@AccountName", account.AccountName);
                cmd.Parameters.Add("@Password", account.Password);
                cmd.Parameters.Add("@Email", account.Email);
                cmd.Parameters.Add("@Phone", account.Phone);
                cmd.Parameters.Add("@Role", account.Role);
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                // show dialog
                return 0;
            }
            finally
            {
                conn.Close();
            }
        }

        public int EditAccountAdmin(EntityAccountAdmin account)
        {
            try
            {
                account = new EntityAccountAdmin();
                conn = AppConfig.GetConnection();
                SqlCommand cmd = new SqlCommand("proc_name", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@FullName", account.FullName);
                cmd.Parameters.Add("@AccountName", account.AccountName);
                cmd.Parameters.Add("@Email", account.Email);
                cmd.Parameters.Add("@Phone", account.Phone);
                cmd.Parameters.Add("@Role", account.Role);
                cmd.Parameters.Add("@Id", account.Id);
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                // show dialog
                return 0;
            }
            finally
            {
                conn.Close();
            }
        }

        public int DeleteAccountAdmin(int accountId, int role)
        {
            try
            {
                conn = AppConfig.GetConnection();
                SqlCommand cmd = new SqlCommand("proc_name", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Id", accountId);
                cmd.Parameters.Add("@Role", role);
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                // show dialog
                return 0;
            }
            finally
            {
                conn.Close();
            }  
        }

        public int NewAccountUser(EntityAccountUser account)
        {
            try
            {
                account = new EntityAccountUser();
                conn = AppConfig.GetConnection();
                SqlCommand cmd = new SqlCommand("proc_name", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@FullName", account.FullName);
                cmd.Parameters.Add("@Email", account.Email);
                cmd.Parameters.Add("@Password", account.Password);
                cmd.Parameters.Add("@Work", account.Work);
                cmd.Parameters.Add("@Desc", account.Description);
                cmd.Parameters.Add("@Avata", account.Avata);
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                // show dialog
                return 0;
            }
            finally
            {
                conn.Close();
            }
        }

        public int DeleteAccountUser(int id)
        {
            try
            {
                //account = new EntityAccountUser();
                conn = AppConfig.GetConnection();
                SqlCommand cmd = new SqlCommand("proc_name", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@Id", id);
                conn.Open();
                return cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                // show dialog
                return 0;
            }
            finally
            {
                conn.Close();
            }  
        }
    }
}
